/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
	ListNode *curA = headA;
	ListNode *curB = headB;

	int lenA = 0;
	int lenB = 0;
	while (curA->next) {
		curA = curA->next;
		lenA++;
	}
	while (curB->next) {
		curB = curB->next;
		lenB++;
	}

	if (curA != curB) {
		return NULL;
	}

	ListNode *longhead = (lenA >= lenB) ? headA : headB;
	ListNode *shorthead = (lenA < lenB) ? headA : headB;

	/*ListNode* longhead=headA;
	ListNode* shorthead=headB;
	if(lenA<lenB){
	    longhead=headB;
	    shorthead=headA;
	}*/

	for (int i = 0; i < abs(lenA - lenB); i++) {
		longhead = longhead->next;
	}

	while (longhead != shorthead) {
		longhead = longhead->next;
		shorthead = shorthead->next;
	}

	return longhead;

}